package main

import (
	"flag"
	"fmt"
	"os"
	"strconv"
	"time"

	"github.com/360EntSecGroup-Skylar/excelize"
)

var prefix = "jsk3"
var formate = "2006/1/02 15:04:05"

var sheetName = "1014"

func main() {

	//  go run main.go -beginDate=2018/4/02 -days=31
	fmt.Println("run like this :  enter dir  ./sh11x5_darwin_amd64  -beginDate=`2018/4/01` -days=30")
	var days int
	flag.IntVar(&days, "days", 1, "days")
	var beginDate string
	flag.StringVar(&beginDate, "beginDate", "nil", "beginDate")
	flag.Parse()

	if "nil" == beginDate {
		fmt.Println("params -beginDate have to need.")
		os.Exit(-1)
	}
	for i := 1; i <= days; i++ {
		var formateyyymmdd = "2006/1/02"
		local, _ := time.LoadLocation("America/Los_Angeles")
		timeNow, _ := time.ParseInLocation(formateyyymmdd, beginDate, local)
		timeDay1 := timeNow.Add(time.Duration((i-1)*24) * time.Hour)
		date := timeDay1.Format(formateyyymmdd)
		fmt.Println("---->", date)
		excel(date)
	}
}

func excel(beginDate string) {
	//os.Exit(-1)
	local, _ := time.LoadLocation("America/Los_Angeles")

	xlsx := excelize.NewFile()
	// Create a new sheet.
	index := xlsx.NewSheet(sheetName)
	if index > 1 {
		xlsx.DeleteSheet("Sheet1")
	}
	// Set value of a cell.
	//xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
	// Title
	xlsx.SetCellStr(sheetName, "A1", "期数")
	xlsx.SetCellStr(sheetName, "B1", "开始投注时间")
	xlsx.SetCellStr(sheetName, "C1", "开奖时间")
	xlsx.SetCellStr(sheetName, "D1", "是否最后一期")
	max := 83

	inputTime := beginDate //输入需要生成的当天时间

	var formateyyymmdd = "2006/1/02"
	timeNow, _ := time.ParseInLocation(formateyyymmdd, inputTime, local)
	timeA := timeNow.Format("20060102")
	//a:=inputTime*100 // 每天小于100期
	aa, _ := strconv.ParseInt(timeA, 10, 64)
	a := aa * 1000
	b := inputTime + "  8:30:00" //每天开始投注时间

	for i := 2; i <= max; i++ {
		nums := strconv.Itoa(i)
		xlsx.SetCellValue(sheetName, "A"+nums, a+int64((i-1)))

		timeNow, eeee := time.ParseInLocation(formate, b, local)
		if eeee != nil {
			fmt.Println(eeee)
		}
		// B C yyyy/m/d" "hh:mm:ss
		tim10 := timeNow.Add(time.Duration((i-2)*10) * time.Minute)
		xlsx.SetCellValue(sheetName, "B"+nums, tim10)

		tim20 := timeNow.Add(time.Duration((i-1)*10) * time.Minute)
		xlsx.SetCellValue(sheetName, "C"+nums, tim20)
		//0" "
		if i == max {
			xlsx.SetCellInt(sheetName, "D"+nums, 1)
		} else {
			xlsx.SetCellInt(sheetName, "D"+nums, 0)
		}
	}
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)

	// Save xlsx file by the given path.
	err := xlsx.SaveAs("./" + prefix + "_" + timeA + ".xlsx")
	if err != nil {
		fmt.Println(err)
	}
}
